package com.hertz.alarm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hertz.alarm.controller.support.ResultModel;
import com.hertz.alarm.entity.MatchNba;
import com.hertz.alarm.entity.Team;
import com.hertz.alarm.service.FetchNbaData;
import com.hertz.alarm.service.MatchNbaService;
import com.hertz.alarm.service.TeamNbaService;
import com.hertz.alarm.util.Constants;
import com.hertz.alarm.util.DateUtils;
import com.hertz.alarm.util.ReturnCodeConstants;
import com.hertz.alarm.warper.MatchNbaWarper;

@Controller
@RequestMapping("/admin/nmatch")
public class MatchController extends BaseController{

	@Resource
	private MatchNbaService matchNbaService;
	@Resource
	private TeamNbaService teamNbaService;
	@Resource
	private FetchNbaData fetchNbaData;
	
	@RequestMapping(value="list",method=RequestMethod.GET)
	public ModelAndView getTeamNba(
			HttpServletRequest request,
			@RequestParam(value="ymd",required=false,defaultValue="") String ymd){
		if(StringUtils.isBlank(ymd)){
			ymd = DateUtils.today(DateUtils.Y_M_D);
		}
		List<MatchNba> list = matchNbaService.listByYmd(ymd);
		List<MatchNbaWarper> warperList = new ArrayList<MatchNbaWarper>();
		for(MatchNba matchNba : list){
			MatchNbaWarper warper = new MatchNbaWarper();
			warper.setMatchNba(matchNba);
			Team hostTeam = teamNbaService.getTeamBySourceId(String.valueOf(matchNba.getHostTeamId()));
			warper.setHostTeam(hostTeam);
			Team guestTeam = teamNbaService.getTeamBySourceId(String.valueOf(matchNba.getGuestTeamId()));
			warper.setGuestTeam(guestTeam);
			warperList.add(warper);
		}
		Map<String, Object> dataMap = new HashMap<String,Object>();
		dataMap.put("data", warperList);
		dataMap.put("ymd", ymd);
		return returnModelAndView(dataMap, request);
	}
	@RequestMapping(value="fetch",method=RequestMethod.POST)
	public @ResponseBody ResultModel<Object> fetchMatchSchedul(
			HttpServletRequest request,
			@RequestParam(value="ymd",required=false,defaultValue="") String ymd){
		if(StringUtils.isBlank(ymd)){
			ymd = DateUtils.today(DateUtils.Y_M_D);
		}
		fetchNbaData.fetchMatchSchedule(ymd);
		return buildResultModel(null, ReturnCodeConstants.SUCCESS);
	}
}
